//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using CommunityServer.Components;
using CommunityServer.Configuration;

namespace CommunityServer.Discussions.Components
{
	/// <summary>
	/// Summary description for ForumUrls.
	/// </summary>
	public class ForumUrls : CommunityServer.Components.ApplicationUrls
	{

		public static readonly string ForumUrlsProviderName = "ForumUrlsProvider";

		public ForumUrls() { }


		#region Instance

		private static ForumUrls _defaultInstance = null;

		static ForumUrls()
		{
			CreateDefaultCommonProvider();
		}

		/// <summary>
		/// Returns an instance of the user-specified data provider class.
		/// </summary>
		/// <returns>An instance of the user-specified data provider class.  This class must inherit the
		/// CommonDataProvider interface.</returns>
		public static ForumUrls Instance() 
		{
			return _defaultInstance;
		}

		public static ForumUrls Instance (Provider dataProvider) 
		{
			ForumUrls fdp = CSCache.Get(dataProvider.Name) as ForumUrls;
			if(fdp == null)
			{
				fdp = DataProviders.Invoke(dataProvider) as ForumUrls;
				CSCache.Max(dataProvider.Name,fdp);
			}
			return fdp;
		}

		/// <summary>
		/// Creates the Default CommonDataProvider
		/// </summary>
		private static void CreateDefaultCommonProvider()
		{
			// Read the configuration specific information
			// for this provider
			//
			Provider urlProvider = (Provider) CSConfiguration.GetConfig().Providers[ForumUrlsProviderName];

			// Create the instance for the provider, or fall back on the builtin one
			//
			if(urlProvider != null)
				_defaultInstance = CreateInstance(urlProvider) as ForumUrls;
			else
				_defaultInstance = new ForumUrls();
		}
        
		#endregion

		#region public Forum methods
        
        public virtual string Forum (int forumID) 
        {
            if (forumID < 0)
                return string.Empty;

            Section f = Forums.GetForum(forumID, true, true);

            if(f == null)
                f = Forums.GetForum(forumID); //Hack. First attempt fails, but the second works?

            switch (f.ForumType) 
            {
                case ForumType.Link:
                    return f.NavigateUrl;

                case ForumType.FAQ:
                    return FormatUrl("telligentFaq", forumID.ToString());

                case ForumType.KB:
                    return FormatUrl("telligentKb", forumID.ToString());

                default:
  
                    return FormatUrl("forum", forumID.ToString());
  
            }
        }

		public virtual string PostPendingModeration(int forumID)
		{
			return FormatUrl("post_PendingModeration", forumID.ToString());
		}

		#endregion

        public virtual string QuickReply
        {
            get {return FormatUrl("post_QuickReply");}
        }

		public virtual string ForumSubscriptions
		{
			get { return FormatUrl("forumSubscriptions"); }
		}

		public virtual string ThreadNavigation(int postID, ThreadNavigationType type)
		{
			return FormatUrl("thread_Navigation", postID.ToString(), type.ToString());
		}

		public virtual string PostSingle (int postID)
		{
			return FormatUrl("post_Single", postID.ToString());
		}

		public virtual string PostInThreadedView (int threadID, int postID)
		{
			return FormatUrl("post_InThreadedView", threadID.ToString(), postID.ToString());
		}

        public virtual string PostCreate (int forumID) 
        {
            return FormatUrl("post_Create", forumID.ToString());
        }

        public virtual string PostPagedFormat (int postID) 
        {
            return FormatUrl("post_Paged", "{0}", postID.ToString(), postID.ToString());
		}

		public virtual string PostPagedRaw (int postID) 
		{
			return FormatUrl("post_PagedRaw", "{0}", postID.ToString());
		}

        public virtual string PostPaged (int postID, int page) 
        {
            return FormatUrl("post_Paged", page.ToString(), postID.ToString(), postID.ToString());
		}

        public virtual string PostReply (int postID) 
        {
            return FormatUrl("post_Reply",  postID.ToString());
        }

        public virtual string PostInPage (int postID, int postInPageID) 
        {
            return FormatUrl("post_InPage", postID.ToString(), postInPageID.ToString());
        }

		public virtual string PostPermaLink (int postID, int permaPostID) 
		{
			return FormatUrl("post_PermaLink", postID.ToString(), permaPostID.ToString());
		}

        public virtual string PostReply (int postID, bool isQuote) 
        {
            return FormatUrl("post_Quote", postID.ToString(), isQuote.ToString());
        }

        public virtual string PostEdit (int postID, string returnUrl) 
        {
            return FormatUrl("post_Edit", postID.ToString(), returnUrl);
        }

        public virtual string PostDelete (int postID, string returnUrl) 
        {
            return FormatUrl("post_Delete", postID.ToString(), returnUrl);
        }

        public virtual string PostAttachment (int postID) 
        {
			PostAttachmentMetaData pa = PostAttachments.GetPostAttachtmentMetaData(postID);
			if (pa == null)
				return String.Empty;

			if (pa.IsRemote)
				return pa.FileName;
			else
			{	
				ForumAttachmentSettings fas = ForumConfiguration.Instance().AttachmentSettings;

				if (fas.EnableDirectLinks && fas.EnableFileSystemStorage)
					return CSContext.Current.Context.Response.ApplyAppPathModifier(fas.DirectPath(pa));
				else
					return FormatUrl("post_Attachment", postID.ToString());
			}
        }

		public virtual string UploadAttachment (int sectionID)
		{
			return FormatUrl("post_UploadAttachment", sectionID.ToString());
		}

		public virtual string PostsThreadView(ThreadViewMode mode)
		{
			switch (mode)
			{
				case ThreadViewMode.Active:
					return this.PostsActive;
				case ThreadViewMode.MyForums:
					return SiteUrls.Instance().UserMyForums;
				case ThreadViewMode.NotRead:
					return this.PostsNotRead;
				case ThreadViewMode.PrivateMessages:
					return SiteUrls.Instance().UserPrivateMessages;
				case ThreadViewMode.Unanswered:
					return this.PostsUnanswered;
				case ThreadViewMode.Videos:
					return this.PostsVideos;
				default:
					return SiteUrls.Instance().ForumsHome;
			}
		}

        public virtual string PostsUnanswered 
        {
            get 
            { 
                string val = FormatUrl( "post_Unanswered" );
                int pos = val.IndexOf( "?", 0 );
                if (pos > 1)
                    val = val.Substring( 0, pos );

                return val;
            }
        }

        public virtual string PostsUnansweredPaged (int forumID) 
        {
            if (forumID > 0)
            {
                return FormatUrl( "post_Unanswered", forumID.ToString() + "&PageIndex={0}" );
            } 
            else
            {
                string val = FormatUrl( "post_Unanswered", forumID );
                int pos = val.IndexOf( "?", 0 );
                if (pos > 1)
                    val = val.Substring( 0, pos );

                return val + "?PageIndex={0}";
            }
        }

        public virtual string PostsActive 
        {
            get 
            { 
                string val = FormatUrl( "post_Active" );
                int pos = val.IndexOf( "?", 0 );
                if (pos > 1)
                    val = val.Substring( 0, pos );

                return val;
            }
        }

		public virtual string PostsVideos
		{
			get
			{
				string val = FormatUrl("post_Videos");
				int pos = val.IndexOf("?", 0);
				if (pos > 1)
					val = val.Substring(0, pos);

				return val;
			}
		}

        public virtual string PostsNotRead 
        {
            get 
            { 
                string val = FormatUrl( "post_NotRead" );
                int pos = val.IndexOf( "?", 0 );
                if (pos > 1)
                    val = val.Substring( 0, pos );

                return val;
            }
        }

        public virtual string PostsActivePaged (int forumID) 
        {
            if (forumID > 0)
            {
                return FormatUrl( "post_Active", forumID.ToString() + "&PageIndex={0}" );
            } 
            else
            {
                string val = FormatUrl( "post_Active", forumID );
                int pos = val.IndexOf( "?", 0 );
                if (pos > 1)
                    val = val.Substring( 0, pos );

                return val + "?PageIndex={0}";
            }
        }

        public virtual string PrintPost (int postID) 
        {
            return FormatUrl("post_Print", postID.ToString());
        }

        public virtual string PostsNotReadPaged (int forumID) 
        {
            if (forumID > 0)
            {
                return FormatUrl( "post_NotRead", forumID.ToString() + "&PageIndex={0}" );
            } 
            else
            {
                string val = FormatUrl( "post_NotRead", forumID );
                int pos = val.IndexOf( "?", 0 );
                if (pos > 1)
                    val = val.Substring( 0, pos );

                return val + "?PageIndex={0}";
            }
        }

        public virtual string RssForum (int forumID, ThreadViewMode mode) 
        {
            return FormatUrl("rss", forumID.ToString(), ((int) mode).ToString());
        }

		public virtual string RssForumThreadless (int sectionID) 
		{
			return FormatUrl("rss_threadless", sectionID.ToString());
		}
		
		public virtual string RssPost (int forumID, int postID) 
        {
            return FormatUrl("rss_post", forumID.ToString(), postID.ToString());
        }

		public virtual string RssAggregate(ThreadViewMode mode)
		{
			return FormatUrl("rss_aggregate", ((int)mode).ToString());
		}

		public virtual string CommentRss(int sectionID, int postID)
		{
			return FormatUrl("forumCommentRss", sectionID, postID);
		}

		public virtual string EmailInformation (int sectionID)
		{
			return FormatUrl("email_information", sectionID);
		}

		public virtual string NntpInformation
		{
			get { return FormatUrl("nntp_information"); }
		}


		#region Report
		public virtual string Report(int reportedSectionID)
		{
			return FormatUrl("post_Create", reportedSectionID.ToString());
		}
		#endregion
        
		#region ConrolPanel Urls

		public virtual string ControlPanel_ManageForums
		{
			get{ return FormatUrl("forums_ControlPanel_ManageForums");}
		}

		public virtual string ControlPanel_ForumList
		{
			get{ return FormatUrl("forums_ControlPanel_ForumList");}
		}

		public virtual string ControlPanel_ForumListByGroup(int groupID)
		{
			return FormatUrl("forums_ControlPanel_ForumListByGroup", groupID);
		}

		public virtual string ControlPanel_GroupList
		{
			get{ return FormatUrl("forums_ControlPanel_GroupList");}
		}
		
		public virtual string ControlPanel_NewForum
		{
			get{ return FormatUrl("forums_ControlPanel_NewForum");}
		}

		public virtual string ControlPanel_EditForum(int sectionID)
		{
			return FormatUrl("forums_ControlPanel_EditForum", sectionID);
		}

		public virtual string ControlPanel_NewGroup
		{
			get{ return FormatUrl("forums_ControlPanel_NewGroup");}
		}

		public virtual string ControlPanel_EditGroup(int groupID)
		{
			return FormatUrl("forums_ControlPanel_EditGroup", groupID);
		}

		public virtual string ControlPanel_DefaultPermissionList
		{
			get{ return FormatUrl("forums_ControlPanel_DefaultPermissionList");}
		}

		public virtual string ControlPanel_PostOptions
		{
			get{ return FormatUrl("forums_ControlPanel_PostOptions");}
		}

		public virtual string ControlPanel_ManageRanks
		{
			get{ return FormatUrl("forums_ControlPanel_ManageRanks");}
		}
		
		#endregion
	}
}
